<template>
 <div class="ratingselect">
   <div class="rating-type border-1px">
      <!--当具有两个参数的时候,必须设置select(2,$event)而不是select(2)这种形式 -->
      <span @click="select(2,$event)" class="block positive" :class="{'active':selectType === 2}">{{desc.all}}<span class="count">
        {{ratings.length}}</span></span>
      <span @click="select(0,$event)" class="block positive" :class="{'active':selectType === 0}">{{desc.positive}}<span class="count">
        {{positives.length}}</span></span>
      <span @click="select(1,$event)" class="block negative" :class="{'active':selectType === 1}">{{desc.negative}}<span class="count">
        {{negatives.length}}</span></span>
    </div>
   <!--选择check -->
   <div @click="toggleContent" class="switch" :class="{'on':onlyContent}">
    <span class="icon-check_circle"></span>
    <span class="text">只看有内容的评价</span>
   </div>
 </div>
</template>

<script>
const POSITIVE = 0;//满意
const NEGATIVE = 1;//不满意
const ALL = 2;//全部

export default {
  props: {
    ratings: {
      type: Array, //数组的返回值是函数
      default() {
        return [];
      }
    },
    selectType: { //选择类型
      type: Number,
      default: ALL
    },
    onlyContent:{ //是否可读
      type: Boolean,
      default: false
    },
    desc: { //选择类型的描述
      type: Object,
      default() {
        return {
        all:'全部',
        positive:'满意',
        negative:'不满意'
        };
      }
    }
  },
  data() {
    return {};  
  },
  created() {
      //初始化加载ratingselect子组件内容
     console.log('初始化加载ratingselect子组件内容 ',this.ratings);
     console.log(this.desc);
  },
  computed:{
    //正面评价总数
    positives() {
      return this.ratings.filter((rating) =>{
         return rating.rateType === POSITIVE; 
      });
    },
    //反面评价总数,通过过滤方法
    negatives() {
      return this.ratings.filter((rating) =>{
         return rating.rateType === NEGATIVE;  
      })
    }
  },
  methods: {
    //触发全部，推荐，吐槽
    select(type,event){
      //兼容pc
      console.log('select中的传递参数type  ' + type);
      if(!event._constructed){
        return;
      } 
      //子组件selectType发生变化，要通知父组件food中对应的selectType发生变化
      this.$emit('selectType',type);
    },
    toggleContent(event){
      //兼容pc
      if(!event._constructed){
        return;
      }  
      this.$emit('onlyContent',this.onlyContent);//通知父组件food中对应的selectType发生变化,通过状态管理vuex进行实现
    }
  }
}
</script>

<style lang="scss" rel="stylesheet/scss">
@import "../../../assets/style/mixin.scss";
.ratingselect {
  .rating-type {
    padding: 18px 0;
    margin: 0 18px;
    @include border-1px(rgba(7, 17, 27, 0.1));
    .block {
      display: inline-block;
      padding: 8px 12px;
      margin-right: 8px;
      border-radius: 2px;
      line-height: 16px;
      color: rgb(77,85,93);
      &.active {
        color: #fff;
      }
      .count {
        font-size: 10px;
        margin-left: 2px;
      }
      &.positive {
        background: rgba(0, 160, 220,0.2);
        &.active {
          background: rgb(0, 160, 220);
        }
      }
      &.negative {
        background: rgba(77, 85, 93,0.2);
        &.active {
          background: rgb(77, 85, 93);
        }
      }
    }
  }
  .switch {
    padding: 12px 0 ;
    margin: 0 18px;
    line-height: 24px;
    border-bottom: 1px solid rgba(7, 17, 27, 0.1);
    color: rgb(147, 153, 159);
    &.on { //选中状态
      .icon-check_circle {
       color: #00c850;
      }
    }
    .icon-check_circle {
      display: inline-block;
      vertical-align: top;
      margin-right: 4px;
      font-size: 24px;
    }
    .text {
      display: inline-block;
      vertical-align: top;
      font-size: 12px;
    }
  }
}
</style>
